﻿using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using WholeSlideImageViewer;

namespace Microsoft.AspNetCore.Builder
{
    public static class DeepZoomRouteBuilderExtensions
    {
        public static void MapDeepZoom(this IEndpointRouteBuilder endpoints)
        {
            if (endpoints is null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }

            endpoints.MapGet("/deepzoom/{id}.dzi", async context =>
            {
                IDeepZoomImageSource? dz = context.RequestServices.GetService<IDeepZoomImageSource>();
                HttpResponse response = context.Response;
                string? id = context.Request.RouteValues["id"].ToString();
                if (id is null)
                {
                    response.StatusCode = StatusCodes.Status404NotFound;
                    return;
                }
                string? dziFileContent = await dz.GetDziContentAsync(id);
                if (dziFileContent is null)
                {
                    response.StatusCode = StatusCodes.Status404NotFound;
                }
                else
                {
                    response.ContentType = "application/xml";
                    await response.WriteAsync(dziFileContent);
                }
            });
            endpoints.MapGet("/deepzoom/{id}_files/{layer:int}/{col:int}_{row:int}.{format}", async context =>
            {
                IDeepZoomImageSource? dz = context.RequestServices.GetService<IDeepZoomImageSource>();

                string? id = context.Request.RouteValues["id"].ToString();
                string? layer = context.Request.RouteValues["layer"].ToString();
                string? col = context.Request.RouteValues["col"].ToString();
                string? row = context.Request.RouteValues["row"].ToString();
                string? format = context.Request.RouteValues["format"].ToString();

                HttpResponse response = context.Response;
                if (id is null || layer is null || col is null || row is null || format is null)
                {
                    response.StatusCode = 404;
                    return;
                }
                if (!int.TryParse(layer, out int l) || !int.TryParse(col, out int c) || !int.TryParse(row, out int r))
                {
                    response.StatusCode = 404;
                    return;
                }

                await dz.SendTileAsync(id, l, c, r, format, response);
            });
            endpoints.MapGet("/associated/{id}/{name}.jpg", async context =>
            {
                IDeepZoomImageSource? dz = context.RequestServices.GetService<IDeepZoomImageSource>();

                string? id = context.Request.RouteValues["id"].ToString();
                string? name = context.Request.RouteValues["name"].ToString();

                HttpResponse response = context.Response;

                if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(name))
                {
                    response.StatusCode = 404;
                    return;
                }

                await dz.SendAssociatedImageJpegAsync(id, name, response);
            });
        }
    }
}
